package com.peacebird.quick.job.service.impl;

import com.peacebird.quick.job.mapper.TradeImportMapper;
import com.peacebird.quick.job.model.BasTradeImport;
import com.peacebird.quick.job.service.TradeImportService;
import com.peacebird.utils.contants.ComContants;
import com.peacebird.utils.dto.ResultDto;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ZYZ
 * @DateTime: 2020/3/2 14:58
 * @Description:
 */
@Service

public class TradeImportServiceImpl implements TradeImportService {

    @Autowired
    TradeImportMapper tradeImportMapper;

    private final String INPORTMENU = "导入生意参谋数据";


    /**
     * <p>
     *  判断数据是否已经导入生成
     *      1、当前店铺在数据生成时间点是否已经存在数据（包含人工导入和系统自动导入）
     *
     * </p>
     * @param :  * @param importData
     * @param shopid
     * @return: int
     * @author: ZYZ
     * @version : 1.0.0
     * @creation date: 2020/3/3 15:52
     * @modification history: who   when    what
     * ------   ---------   ------------------------------
     **/
    @Transactional
    @Override
    public ResultDto insertTradeImportNew(List<BasTradeImport> importData, String shopId) throws Exception{


        if(null != importData && importData.size() > 0 && null!= shopId){
            Date nowDate = new Date();

            if(importData.size()<= ComContants.BIG_TABLE_BATH_COUNT){
                tradeImportMapper.insertTradeImportNew(importData, nowDate, ComContants.SYSTEM,INPORTMENU);
            }else{
                int times = (int)Math.ceil( importData.size()/Double.valueOf(ComContants.BIG_TABLE_BATH_COUNT) );
                for(int i=0; i<times; i++ ){
                    int count = tradeImportMapper.insertTradeImportNew(importData.subList (i*ComContants.BIG_TABLE_BATH_COUNT,Math.min((i+1)*ComContants.BIG_TABLE_BATH_COUNT, importData.size())), nowDate, ComContants.SYSTEM,INPORTMENU);

                    XxlJobLogger.log("分批插入:{} ,插入行数 : {} ", i , count) ;
                }
            }

            Map<String, Object> params = new HashMap<String, Object>();
            params.put( "userid", ComContants.SYSTEM);
            params.put( "deptid", "");
            params.put( "shopid", shopId);
            params.put( "code", "");
            params.put( "msg", "");
            tradeImportMapper.executTradeImportNew(params);
            if(ComContants.STR_Y.equals(params.get("code"))){
                return ResultDto.success();
            }else{
                return ResultDto.error(ComContants.DEFAULT_ERROR_CODE, params.get("msg").toString());
            }

        }
        return ResultDto.error(ComContants.DEFAULT_ERROR_CODE,ComContants.ERROR_MSG_NULL);

    }

    @Override
    public int queryTradeRepition(String shopId, Date dataDate) {
        if (null == shopId || null == dataDate){
            return 0;

        }
        Integer count = tradeImportMapper.queryTradeRepetition(shopId,dataDate);

        return count;
    }

    @Override
    public String checkShopId(String shopId) {
        String shopName = tradeImportMapper.checkShopId(shopId);

        return shopName;
    }


}
